package app.server.project.cloud.random.db.entity;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "room")
@TableName("room")
@Comment(value = "房间信息表")
public class RoomEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("user_id")
    @Comment("用户id")
    @Column(name = "user_id")
    private long userId;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @TableField("name")
    @Comment("房间名称")
    @Column(name = "name")
    private String name;





    @TableField("is_del")
    @Comment("删除")
    @Column(name = "is_del")
    private int isDel;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RoomEntity::getCreateTime, this);
    }

    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RoomEntity::getUserId, this);
    }

    public void setName(String name) {
        this.name = name;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RoomEntity::getName, this);
    }

    public void setIsDel(int isDel) {
        this.isDel = isDel;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RoomEntity::getIsDel, this);
    }

    public static RoomEntity of(long id,long userId,String name){
        var ret=new RoomEntity();
        ret.setSynDb(true);
        ret.setId(id);
        ret.setIsDel(Def.FALSE);
        ret.setName(name);
        ret.setUserId(userId);
        ret.setCreateTime(LocalDateTime.now());

        return ret;
    }

}
